package com.foreverwin.mes.meapi.service.impl;

import com.foreverwin.mes.core.base.FrontPage;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.foreverwin.mes.core.utils.TimeUtils;
import com.foreverwin.mes.meapi.dto.ShiftResult;
import com.foreverwin.mes.meapi.model.UserShift;
import com.foreverwin.mes.meapi.mapper.UserShiftMapper;
import com.foreverwin.mes.meapi.service.UserShiftService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Syngna
 * @since 2020-01-13
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class UserShiftServiceImpl extends ServiceImpl<UserShiftMapper, UserShift> implements UserShiftService {


    @Autowired
    private UserShiftMapper userShiftMapper;

    @Override
    public Page<UserShift> selectPage(FrontPage<UserShift> frontPage, UserShift userShift) {
        EntityWrapper<UserShift> queryWrapper = new EntityWrapper<>();
        queryWrapper.setEntity(userShift);
        return super.selectPage(frontPage.getPagePlus(), queryWrapper);
    }

    @Override
    public List<UserShift> selectList(UserShift userShift) {
        EntityWrapper<UserShift> queryWrapper = new EntityWrapper<>();
        queryWrapper.setEntity(userShift);
        return super.selectList(queryWrapper);
    }

    @Override
    public List<ShiftResult> getUserShiftList(String site) {

        List<ShiftResult> list = userShiftMapper.selectUserShift( site);
        if( list.size() > 0 ){
            for( int i = 0;i < list.size() ;i ++){
                ShiftResult shiftResult = list.get(i);
                String shiftBegin = shiftResult.getShiftBegin();
                shiftBegin = TimeUtils.stringToTime( shiftBegin);
                shiftResult.setShiftBegin(shiftBegin);

                String shiftEnd = shiftResult.getShiftEnd();
                shiftEnd = TimeUtils.stringToTime( shiftEnd);
                shiftResult.setShiftEnd(shiftEnd);
            }
        }

        return list;
    }

    @Override
    public ShiftResult getUserShiftDefinitionByShift(String site, String userShift) {
        return userShiftMapper.selectUserShiftDefinitionByShift(site, userShift);
    }
}